﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
namespace DevEx.Thanh.Class
{
    class CKiemKeController
    {
        private QLVTDataContext objDataContext;
        public CKiemKeController ()
        {
            objDataContext = new QLVTDataContext();
        }
        public CKiemKeController (QLVTDataContext _obj)
        {
            objDataContext = _obj;
        }
        public void RefreshData()
        {
            objDataContext = new QLVTDataContext();
        }
        public tblKiemKe SelectRecordByID(int id)
        {
            try
            {
                return (from kk in objDataContext.tblKiemKes where kk.ID_KiemKe == id select kk).Single();
            }
            catch (Exception)
            {
                return null;
            }

        }
        public void Update(tblKiemKe  obj)
        {
            //objDataContext = new QLVTDataContext();
            objDataContext .tblKiemKes.Attach(obj);
            objDataContext .Refresh(RefreshMode.KeepCurrentValues, obj);
            objDataContext .SubmitChanges(ConflictMode.ContinueOnConflict);
        }
        public void UpdateWithoutAttach(tblKiemKe  obj)
        {
            objDataContext .Refresh(RefreshMode.KeepCurrentValues, obj);
            objDataContext .SubmitChanges(ConflictMode.ContinueOnConflict);
        }
        public Boolean CheckIfExist(int dotKiemKe)
        {
            return (objDataContext.tblKiemKes.Any(kk=> kk.DotKiemKe ==dotKiemKe));
        }
        //public IQueryable LayThongTinKiemKe(int dotKiemKe, int kho)
        //{
        //    try
        //    {
        //        return (from kk in objDataContext.tblKiemKes
        //                from t in objDataContext.tblTons
        //                from vt in objDataContext.tblVatTus
        //                from dv in objDataContext.tblDVTs
        //                where
        //                    kk.DotKiemKe == dotKiemKe && kk.ID_Kho==kho && kk.ID_MatHang == t.ID_MatHang && t.ID_VatTu == vt.ID_VatTu &&
        //                    vt.ID_DVT == dv.ID_DVT
        //                select new {KiemKe = kk,vt.VatTu, vt.MaKyHieu, dv.DVT, t.Gia  });
        //    }
        //    catch (Exception)
        //    {
        //        return null;
        //    }
        //}
        public ISingleResult< sp_T_LayThongTinKiemKeResult > LayThongTinKiemKe2(int dotKiemKe)
        {
            try
            {
                return objDataContext.sp_T_LayThongTinKiemKe(dotKiemKe);
            }
            catch (Exception)
            {
                return null;
            }
        }
        public void ThucHienKiemKe(int dotKiemKe)
        {
            if (dotKiemKe == 1)
            {
                objDataContext.sp_T_KiemKe11();
            }
            else
            {
                objDataContext.sp_T_KiemKe17();
            }
        }
    }
}
